<?php

namespace App\Admin\Controllers;

use App\Availability;
use App\Booking;
use App\Order;
use App\Http\Controllers\Controller;
use App\Room;
use App\RoomType;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('订单管理')
            ->description('管理预定记录')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('订单')
            ->description('订单详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('编辑订单信息')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新建')
            ->description('新预定')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order);

        $grid->id('订单号')->sortable();
        $grid->column('类型')->display(function(){
            return $this->availability->room_type->name;
        });
        $grid->customer()->name('客户姓名');
        $grid->customer()->phone('客户电话');
        $grid->customer()->gender('客户性别')->using([0 => '男', 1 => '女'])->sortable();
        $grid->availability()->from('预计入住日期');
        $grid->availability()->to('预计退房日期');
        $grid->column('状态')->display(function(){
            if ($this->cancelled) {
                return '已取消';
            }
            if($this->check_in) {
                return '已入住';
            }
            // 若未到入住日期，显示为等待状态
            $from = $this->availability->from;
            $today = date('Y-m-d');

            if($from < $today){
                return '等待入住';
            }
            return "<a href='" . route('admin.check_in', ['id' => $this->id]) . "' role='button' class='btn btn-primary'>立即入住</a>";
        });

        // filter
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter){
                $filter->equal('cancelled', '已取消')->radio([
                    '' => '所有',
                    true => '是',
                    false => '否',
                ]);
                $filter->equal('check_in', '已入住')->radio([
                    '' => '所有',
                    true => '是',
                    false => '否',
                ]);
            });
            $filter->column(1/2, function ($filter){
                $filter->where(function ($query){
                    $query->whereHas('customer', function ($query){
                        $query->where('name', $this->input);
                    });
                }, '客户姓名');
                $filter->where(function ($query){
                    $query->whereHas('customer', function ($query){
                        $query->where('phone', $this->input);
                    });
                }, '客户电话');
            });
        });

        $grid->disableRowSelector();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Order::findOrFail($id));

        $show->id('Id');
        $show->created_at('Created at');
        $show->updated_at('Updated at');
        $show->cancelled('Cancelled');
        $show->customer_id('Customer id');
        $show->check_in('Check in');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Order);

        $form->switch('cancelled', 'Cancelled');
        $form->number('customer_id', 'Customer id');
        $form->switch('check_in', 'Check in');

        return $form;
    }

    // check-in
    public function check_in(Request $request){
        $id = $request->get('id');
        // get order
        $order = Order::findOrFail($id);
        // get first unreserved room of current type for user
        $room = Room::where('room_type_id', $order->availability->room_type_id)->unreserved()->first();
        // set room to reserved
        $room->is_reserved = true;
        $room->save();
        // copy availability info from current order and bound it to booking info
        // save current date to 'check_in' in booking info
        $avail = new Availability($order->availability->toArray());
        $booking = new Booking([
            'check_in' => $avail->from,
            'customer_id' => $order->customer_id,
        ]);
        $booking->room()->associate($room);
        $booking->save();
        $booking->availability()->save($avail);
        // set order info's 'check_in' to true
        $order->check_in = true;
        // set order's availability info to invalid
        $order->availability->is_valid = false;
        $order->availability->save();
        $order->save();

        return back();
    }
}
